from pymongo import MongoClient
import os
import json
from collections import Counter
client = MongoClient(port=27017)
db=client['tiktok']
for col in db.list_collection_names():
    print(col)
    d={}
    pct={}
    for obj in db[col].find():
       d[obj['_id']]=''
    datalist=[]
    for fname in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        if col in fname:
            datalist.append(fname)
    for fname in datalist:
        with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + fname, 'r', encoding='utf-8', newline='\n') as filename_input:
            ranking = 0
            c=Counter()
            ids={}
            for line in filename_input:
                try:
                    z = json.loads(line)
                except:
                    continue
                if 'id' in z.keys():
                    id = z['id']
                    mid=z['music']['id']
                elif 'itemInfos' in z.keys():
                    id = z['itemInfos']['id']
                    mid=z['musicInfos']['musicId']
                ids[id]=mid
                c[mid]+=1
                ranking+=1
                if ranking>2000:
                    break
            for id in ids:
                if id in d.keys() and d[id]=='':
                    d[id]=ids[id]
                    pct[id]=c[ids[id]]/ranking
            if len(d)==len(pct):
                break
    for id in d.keys() :
        if id in pct.keys():
            db[col].update_one({'_id': id}, {'$set': {'video_feature.audio.music_id':d[id],'video_feature.audio.percentage':pct[id]}})